window.onload = function () {
    refresh();
    update();
    // 用户信息滚动
    ScrollReveal().reveal('.card', {
        distance: '380px',
        // 持续时间
        duration: 1200,
        // 动画效果
        easing: 'ease-in-out',
        origin: 'bottom',
        // opacity: 0.6,
        // reset: true
    })

    // 用户信息滚动
    ScrollReveal().reveal('.borrow-books-remaining', {
        distance: '380px',
        // 持续时间
        duration: 1400,
        // 动画效果
        easing: 'ease-in-out',
        origin: 'bottom',
        // opacity: 0.6,
        // reset: true
    })

    // 借书证信息
    ScrollReveal().reveal('.return-book', {
        distance: '380px',
        // 持续时间
        duration: 1000,
        // 动画效果
        easing: 'ease-in-out',
        origin: 'top',
        // opacity: 0.6,
        // reset: true
    })
    // 还书弹框
    var functionalborrow = document.querySelector('.importants').getElementsByTagName('a');
    console.log("借书总数---", functionalborrow.length);
    // 获取还书按钮
    // var borrow = document.querySelector('.borrow');

    // 获取还书遮盖层
    var container = document.querySelector('.container');
    // 获取确认按钮
    var determine = document.querySelector('#determine');
    // 获取还书成功
    var container1 = document.querySelector('.container1');
    // 获取还书失败
    var container2 = document.querySelector('.container2');

    // 还书按钮点击事件
    $(document).on("click",".borrow",function (e) {
        console.log($(this).attr("value"));
        var no = $(this).attr("value");
        // 显示弹框
        // container.style.display = "block";
        $('.container').show();

        var bookNo = {
            "bookNo": no
        }
        // 点击确认按钮
        $('#determine').off('click').click(function () {
        // determine.onclick = function () {
            $.ajax({
                type: "get",
                url: '/returnBook',
                dataType: "json",
                contentType: 'application/json',
                data:bookNo,
                async:false,
                success(data) {
                    // 用这个，传过来的值进行判断
                    if (data == true) {
                        // container.style.display = 'none';
                        $('.container').hide();
                        var success = setTimeout(function () {
                            // container1.style.display = 'block';
                            $('.container1').show();
                        }, 800)
                        var success = setTimeout(function () {
                            // container1.style.display = 'none';
                            $('.container1').hide();
                        }, 1800)
                        refresh();
                        update();
                    } else {
                        // container.style.display = 'none';
                        $('.container').hide();
                        var fail = setTimeout(function () {
                            // container2.style.display = 'block';
                            $('.container2').show();
                        }, 800)
                        var fail = setTimeout(function () {
                            // container2.style.display = 'none';
                            $('.container2').hide();
                        }, 1800)
                        refresh();
                        // window.location.reload();
                    }
                },
                error() {
                    console.log("错了",name);
                }


            })
                // setTimeout('refresh()',500);
        // }

        })
    })

    $('#cancel').click(function() {
        $('.container').hide();
    })

//----------------------------
    function refresh() {
        $.ajax({
            url: "/lc",
            dataType: "json",
            contentType: 'application/json',
            type: "get",
            success(data) {
                var text = "";
                for (var i in data) {
                    console.log("no:", data[i].no);
                    text += "<tr><td>" + data[i].bookName + "</td>"
                        + "<td> " + data[i].author + "</td>"
                        + "<td >" + data[i].startTime + "</td>"
                        + "<td >" + data[i].endTime + "</td>"
                        // + "<td>" +data[i].isReturn+ "</td>"
                        + "<td >" + "<button  id='borrow' class='borrow' value='" + data[i].no + "'> 还书</button>" + "</td>" + "</tr>"
                    // $('#tbody').append(text);
                }
                $('#tbody').html(text);

            }

        })
    }

    // 借书数量
    function update() {
        $.ajax({
            type:'get',
            url:'/selectNum',
            dataType:'json',
            success(data) {
                let text = '';
                text = "<p>剩余可借数量:" + data+ "</P>";
                $('.remainder').html(text);
            }
        })
    }

}


